package com.clive.xxweb.controller;

import com.clive.xxweb.bean.Dish;
import com.clive.xxweb.service.DishService;
import com.clive.xxweb.vo.DishResult;
import com.clive.xxweb.vo.DishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dish")
@CrossOrigin
public class DishController {
//   从spring容器中把接口的唯一子类拿出来给service
    @Autowired
    private DishService dishService;
    @RequestMapping("/addDish")
//                                         jsr303注解
    public DishResult addDish(@RequestBody @Valid Dish dish) {
        int i = dishService.addDish(dish);
        return DishResult.ok().data("i",i);
    }
    @RequestMapping("/findOne/{id}")
    public DishResult findDishOne(@PathVariable Integer id){
        Dish dish = dishService.findDishOne(id);
        return DishResult.ok().data("dish",dish);
    }
    @RequestMapping("/update")
    public DishResult updateDish(@RequestBody @Valid Dish dish){
         int i= dishService.updateDish(dish);
         return DishResult.ok().data("i",i);
    }
    @RequestMapping("/delete/{id}")
    public DishResult deleteDish(@PathVariable Integer id){
         int i = dishService.deleteDish(id);

        return DishResult.ok().data("i",i);
    }
    @RequestMapping("/query/{currentPage}/{limit}")
    public DishResult queryDish(@RequestBody Dish dish,@PathVariable Integer currentPage,@PathVariable Integer limit){
        DishVo vo = dishService.findqueryDish(currentPage,limit,dish);
        return DishResult.ok().data("dishList",vo);
    }
}
